package com.sttk.secreconeclick.bookkeep.adapter;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lzb.basic.ProviderMultiAdapter;
import com.lzb.basic.utils.SPUtil;
import com.sttk.secreconeclick.bookkeep.Content;
import com.sttk.secreconeclick.bookkeep.R;
import com.sttk.secreconeclick.bookkeep.bean.BillBean;

import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;


public class SearchAdapter extends ProviderMultiAdapter<BillBean> {

    public SearchAdapter() {
        addItemProvider(new SearchItemProvider());
    }

    private class SearchItemProvider extends BaseItemProvider<BillBean> {
        @Override
        public int getItemViewType() {
            return ProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_search;
        }

        @Override
        public void convert(@NotNull BaseViewHolder baseViewHolder, BillBean bean) {
            baseViewHolder.setImageResource(R.id.ivSearchItemIcon, bean.getBillTypeBean().getSaveIcon());
            baseViewHolder.setText(R.id.tvSearchItemType, bean.getBillTypeBean().getName());
            baseViewHolder.setText(R.id.tvSearchItemDate, TimeUtils.millis2String(bean.getDate(),new SimpleDateFormat("yyyy-MM-dd")));
            if (bean.isIncome()) {
                int colorPos = SPUtil.getInt(getContext(), Content.moneyColor, 0);
                baseViewHolder.setTextColor(R.id.tvSearchItemMoney, colorPos == 0 ? Color.parseColor("#00B868") : Color.parseColor("#C40825"));
                baseViewHolder.setText(R.id.tvSearchItemMoney, "+" + String.format("%.2f", bean.getMoney()));
            } else {
                baseViewHolder.setTextColor(R.id.tvSearchItemMoney, Color.parseColor("#323433"));
                baseViewHolder.setText(R.id.tvSearchItemMoney, "-" + String.format("%.2f", bean.getMoney()));
            }
            if (TextUtils.isEmpty(bean.getImg())) {
                baseViewHolder.getView(R.id.ivSearchItemImg).setVisibility(View.GONE);
            } else {
                baseViewHolder.getView(R.id.ivSearchItemImg).setVisibility(View.VISIBLE);
            }
        }
    }
}
